package com.dingpeikeji.quickdeer.modules.office.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.modules.office.entity.TCompAgreerateGroup;
import com.dingpeikeji.quickdeer.modules.office.service.TCompAgreerateGroupService;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCompAgreerate;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCompAgreerateService;

/**
 * 分组公司Controller
 * @author dingpei
 * @version 2016-12-08
 */
@Controller
@RequestMapping(value = "${adminPath}/office/tCompAgreerateGroup")
public class TCompAgreerateGroupController extends BaseController {

	@Autowired
	private TCompAgreerateGroupService tCompAgreerateGroupService;
	
	@Autowired
	private TCompAgreerateService tCompAgreerateService;
	
	@ModelAttribute
	public TCompAgreerateGroup get(@RequestParam(required=false) String compid) {
		TCompAgreerateGroup entity = null;
		if (StringUtils.isNotBlank(compid)){
			entity = tCompAgreerateGroupService.get(compid);
		}
		if (entity == null){
			entity = new TCompAgreerateGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("rentproduct:tCompAgreerate:edit")
	@RequestMapping(value = {"list", ""})
	public String list(TCompAgreerateGroup tCompAgreerateGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TCompAgreerateGroup> page = tCompAgreerateGroupService.findPage(new Page<TCompAgreerateGroup>(request, response), tCompAgreerateGroup); 
		model.addAttribute("page", page);
		model.addAttribute("agreerateid", tCompAgreerateGroup.getAgreerateid());
		
		TCompAgreerate tCompAgreerate = tCompAgreerateService.get(tCompAgreerateGroup.getAgreerateid().toString());
		
		model.addAttribute("groupname", tCompAgreerate.getParnertype());
		return "modules/office/tCompAgreerateGroupList";
	}

	@RequiresPermissions("rentproduct:tCompAgreerate:edit")
	@RequestMapping(value = "form")
	public String form(TCompAgreerateGroup tCompAgreerateGroup, Model model) {
		model.addAttribute("tCompAgreerateGroup", tCompAgreerateGroup);
		return "modules/office/tCompAgreerateGroupForm";
	}

	@RequiresPermissions("rentproduct:tCompAgreerate:edit")
	@RequestMapping(value = "save")
	public String save(TCompAgreerateGroup tCompAgreerateGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tCompAgreerateGroup)){
			return form(tCompAgreerateGroup, model);
		}
		tCompAgreerateGroupService.save(tCompAgreerateGroup);
		addMessage(redirectAttributes, "保存分组公司成功");
		return "redirect:"+Global.getAdminPath()+"/office/tCompAgreerateGroup/?repage&agreerateid=" + tCompAgreerateGroup.getAgreerateid();
	}
	
	@RequiresPermissions("rentproduct:tCompAgreerate:edit")
	@RequestMapping(value = "delete")
	public String delete(TCompAgreerateGroup tCompAgreerateGroup, RedirectAttributes redirectAttributes) {
		tCompAgreerateGroupService.delete(tCompAgreerateGroup);
		addMessage(redirectAttributes, "删除分组公司成功");
		return "redirect:"+Global.getAdminPath()+"/office/tCompAgreerateGroup/?repage&agreerateid=" + tCompAgreerateGroup.getAgreerateid();
	}

}